/**
*	提示弹窗插件
*/

;(function (jQuery) {
	'use strict';

	var Toast = function (options, deferred) {
		var self = this;
		self.element = $($.toast.template.toast);
		self.options = options;
		self.deferred = deferred;

		if (! $(document.body).find('.toast')[0]) {
			$(document.body).append(self.element);
		}			

		//  主题
		if (self.options.theme) {
			self.element.addClass(self.options.theme);
		}
		//  提示内容
		if (self.options.content) {
			self.element.find('.toast-content').html(self.options.content);
		} 
		//  显示动画
		if (self.options.transition) {
			self.element.addClass(self.options.transition);
		}

		//  initialization
		self.display = false;
		self.show();
		return self;
	};


	Toast.prototype = {
		constructor : Toast,

		//  设置居中
		set_center: function () {
			var self = this,
				w_w = $(window).width(),
				w_h = $(window).height(),
				s_w = self.element.width(),
				s_h = self.element.height(),
				w_sc = $(window).scrollTop();
			self.element
				.addClass('center')
				.css({
					'left': (w_w - s_w) / 2, 
					'top': (w_h - s_h) / 2 + w_sc,
				});
			return self;
		},

		//  显示
		show: function () {
			var self = this, 
				e = $.Event('show.toast');
			if (self.display) {
				return;
			}

			if (self.options.center) {
				self.set_center();
			}
      		self.element.trigger(e).css('display', 'block');
			if (self.options.transition) {
		    	//  force reflow
        		self.element[0].offsetWidth;
        		self.element.addClass('in');
      		}
      		$(document.body).addClass('lock');
      		//  移动端锁屏
      		$(document.body).on('touchmove.lock', function (e) {
      			e.preventDefault();
      		})
      		//  添加遮罩
      		if (self.options.backdrop) {
      			$(document.body).append(
      				$($.toast.template.backdrop).attr('data-backdrop', 'toast')
      			);
      		}
			self.display = true;
			self.timer = setTimeout(function () {
				self.hide();
			}, self.options.duration);
			return self;
		},

		//  隐藏
		hide: function () {
			var self = this, 
				e =  $.Event('hide.toast');
			if (!self.display) {
				return;
			}
			if (self.options.transition) { 
				//  动画结束
				self.element.one('transitionend, webkitTransitionEnd', function () {
		            self.element.removeClass('out').css('display', 'none');
		            $('.toast-backdrop').remove();
					$(document.body).removeClass('lock');
					$(document.body).off('.lock');
		            self.display = false;
		            self.destroy();
	        	});
				self.element.trigger(e).removeClass('in').addClass('out');
				//  force reflow
				self.element[0].offsetWidth; 
      		}
      		else {
      			self.element.trigger(e).css('display', 'none');
      			$('.toast-backdrop').remove();
				$(document.body).removeClass('lock');
				$(document.body).off('.lock');
				self.display = false;
				self.destroy();
      		}
			if (self.deferred) {
	        	self.deferred.resolve(true);
	        }
	        function remove_ele() {

	        }
			return self;
		},

		//  移除
		destroy: function () {
			var self = this;
			if (! self.display) {
				self.element.off('.toast').remove();
				self = null;
				return false;
			}
			else {
				self.hide();
			}
		}
	};

	//  调用
	$.toast = function (options) {
		if (typeof options === 'string') {
			options = {
				content : options
			};
		}
		options = $.extend({}, $.toast.defaults, options);
		var deferred = $.Deferred(),
			toast = new Toast(options, deferred);
		return deferred;
	};

	//  模板
	$.toast.template = {
		toast: '<div class="toast"><div class="toast-content"></div></div>',
		backdrop: '<div class="toast-backdrop"></div>',
	};

	//  默认参数
	$.toast.defaults = {
		theme: '',
		content: 'this is a toast',
		backdrop: true,
		duration: 2000,
		transition: 'scale',
		center: false,
	};

}($));
